package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.myshop.SalePiFaAfterDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.ShopInPiFaOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopInGoodListPresenter;

/**
 * @author gzk
 * @date 2020/5/18 0018
 * 商家入驻  批发订单管理的列表
 * description：
 */
public class ShopInPiFaOrderAfterSalesListAdapter extends RecyclerView.Adapter {


    private List<ShopInPiFaOrderAfterSalesListBeen.DataBean> mList;
    private ItemClickListener itemClickListener;


    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private ShopInGoodListPresenter mPresenter;
    private String styleType;
    RoundedCorners roundedCorners;
    RequestOptions options;

    public ShopInPiFaOrderAfterSalesListAdapter(Context mContext, List<ShopInPiFaOrderAfterSalesListBeen.DataBean> list, String styleType) {
        this.mContext = mContext;
        this.mList = list;

        this.styleType = styleType;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);

        roundedCorners = new RoundedCorners(2);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ShopInPiFaOrderAfterSalesListAdapter.OrderViewHolder0(mContext, mLayoutInflater.inflate(R.layout.item_shop_in_pifa_after_sale_list, null));


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        ShopInPiFaOrderAfterSalesListAdapter.OrderViewHolder0 orderViewHolder0 = (ShopInPiFaOrderAfterSalesListAdapter.OrderViewHolder0) holder;
        orderViewHolder0.setData(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, SalePiFaAfterDetailsActivity.class);
                intent.putExtra("id",mList.get(position).getApply().getId());
                mContext.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class OrderViewHolder0 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_shop)
        ImageView ivShop;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_order_state)
        TextView tvOrderState;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.tv_total_money)
        TextView tvTotalMoney;
        @BindView(R.id.tv_total_num)
        TextView tvTotalNum;
        @BindView(R.id.rv_order_goods)
        RecyclerView rvOrderGoods;


        private Context mContext;

        public OrderViewHolder0(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;

        }

        public void setData(int position) {
            tvShopName.setText(mList.get(position).getUser().getUser_name());
//
            if (mList.get(position).getApply().getStatus() == 1) {
                tvOrderState.setText("等待平台审核");
            } else if (mList.get(position).getApply().getStatus() == 2) {
                tvOrderState.setText("平台审核通过");
            } else if (mList.get(position).getApply().getStatus() == 3) {
                tvOrderState.setText("平台审核驳回");
            } else if (mList.get(position).getApply().getStatus() == 4) {
                tvOrderState.setText("用户已取消");
            }  else if (mList.get(position).getApply().getStatus() == 5) {
                tvOrderState.setText("等待平台审核");
            } else if (mList.get(position).getApply().getStatus() == 6) {
                tvOrderState.setText("退款成功");
            }
            Glide.with(mContext).load(mList.get(position).getUser().getAvatar()).apply(options.transform(new CircleCrop(), roundedCorners)).into(ivShop );

            tvTotalNum.setText("共"+mList.get(position).getApply().getApply_goods_num() + "件");
            tvTotalMoney.setText("¥"+mList.get(position).getApply().getApply_price());

// 商品图
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvOrderGoods.setLayoutManager(manager);
            ShopInPiFaOrderAfterSalesImageAdapter shopInOrderAfterSalesImageAdapter = new ShopInPiFaOrderAfterSalesImageAdapter(mContext, mList.get(position).getGoods());
            rvOrderGoods.setAdapter(shopInOrderAfterSalesImageAdapter);
            shopInOrderAfterSalesImageAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int i) {
                    Intent intent=new Intent(mContext, SalePiFaAfterDetailsActivity.class);
                    intent.putExtra("id",mList.get(position).getApply().getId());
                    mContext.startActivity(intent);
                }
            });



        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
